<?php
class GoodsControl extends AuthControl{
	public function index(){
		$model = K('Goods');
		$m = $model->all();

		$page = new Page($model->count(),10);
		$data = $model->limit($page->limit())->all();
		$this->assign('page',$page->show(3));
		$this->assign('data',$data);
		$this->display();
	}

	public function add(){
		$gtid = Q('gtid');
		if(IS_POST &&isset($gtid)){
			$model = K('Goods');
			$data = $_POST;			
			if(isset($_POST['spec'])){
				$price = $_POST['spec']['price'];
				sort($price);
				$data['price'] = $price[0];
			}	
			if($model->addGoods($data)){

				$this->success('商品添加成功',U('index'));
			}else{
				$this->error($model->error);
			}
		}else{
			// $catmodel = K('Category');
			// $category = $catmodel->all();
			// $category = Data::tree($category,'cname');
			$cid = Q('cid',0,'intval');
			$category = catched('category');
			$category = $category[$cid];
			$goods_type = catched('goodsType');
			$goods_type = $goods_type[$category['gtid']];

//添加属性form
			$attrModel = K('AttrForm');
			$attrForm = $attrModel->attrForm($category['gtid']);
//添加规格form
			$specModel = K('AttrValueForm');
			$attrSpecForm = $specModel->specForm($category['gtid']);
			$brand = catched('brand');
			$this->assign('attrForm',$attrForm);
			$this->assign('attrSpecForm',$attrSpecForm);
			$this->assign('brand',$brand);
			$this->assign('goods_type',$goods_type);
			$this->assign('category',$category);
			$this->display();
		}
	}

	public function save(){

		if(IS_POST){
			$data = $_POST;
			if(isset($_POST['spec'])){
				$price = $_POST['spec']['price'];
				sort($price);
				$data['price'] = $price[0];
			}		
			

			$model = K('Goods');
			if($model->saveGoods($data)){
				$this->success('商品信息更新成功',U('index'));
			}else{
				$this->error($model->error);
			}
		}else{
			$model = K('goods');
			$gid = Q('gid');
			$data = $model->where(array('gid'=>$gid))->Field('*,hd_g_goods.keywords AS k,hd_g_goods.description AS dsc')->all();

			$data = current($data);
			$data['flag'] =  explode(',',$data['flag']);
			$cid = $data['cid'];
			$gtid = $data['gtid'];
			$goodsType = catched('goodsType');

			$category = catched('category');
			$category[$cid]['selected'] = "selected=''";
			$category = Data::tree($category,'cname');
			//商品属性显示
			$attrModel= K('AttrFormShow');
			$attrForm = $attrModel->attrFormShow($gid);
			//商品规格：规格，库存 货号 价格
			$stock = K('AttrValueFormShow');
			$attrSpecForm = $stock->showStock($gid);
// p($goodsType);exit;
			//商品图片表操作
			$picModel = K('GoodsPic');
			$pics = $picModel->where(array('goods_gid'=>$gid))->all();


			$this->assign('goodsType',$goodsType);
			$this->assign('attrSpecForm',$attrSpecForm);
			$this->assign('attrForm',$attrForm);
			$this->assign('pics',$pics);
			$this->assign('category',$category);
			$this->assign('data',$data);
			$this->display();
		}
	}
	public function del(){
		$gid = Q('gid',0,'intval');
		$StockModel = K('Stock');
		$GoodsModel = K('Goods');
		//删除该商品库存和库存属性关联
	
		if($StockModel->gidStockDel($gid)&&$GoodsModel->delGoods($gid)){
			$this->success('商品信息删除成功',U('index'));
		}else{
			$this->error($StockModel->error."  ".$GoodsModel->error);
		}

	}

//异步删除 内容页的图片
	public function delPic(){
		$picModel = K('GoodsPic');
		$picid = Q('picid');
		$picModel->delPics($picid);
		exit;
	}

	public function showCategory(){
		$category = catched('category');
		$this->assign('category',$category);
		$this->display();
	}

	//异步删除规格中的库存数据
	public function delstock(){
		$stid = Q('stid');
		//删除该款规格商品 库存数据及库存属性关联表中的数据
		$StockModel = K('Stock');
		$StockModel->delStock($stid);
		echo '该款商品库存删除成功';
		exit;
	}
}
?>
